package com.iss.card.common;

import java.util.Calendar;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.util.Log;

import com.iss.card.model.StoreTypeInfo;
import com.iss.card.model.UserInfo;
import com.iss.card.view.model.CityBean;

public class FunctionUtil {
	private final static String TAG = FunctionUtil.class.getSimpleName();
	private final static String SP_KEY_SELECTED_CITY = "SP_KEY_SELECTED_CITY";
	private final static String SP_KEY_SELECTED_STORE_TYPE = "SP_KEY_SELECTED_STORE_TYPE";
	private final static String SP_KEY_SELECTED_PAGE_INDEX = "SP_KEY_SELECTED_PAGE_INDEX";
	private final static String SP_KEY_CURRENT_LOGIN_USER = "SP_KEY_CURRENT_LOGIN_USER";

	private final static String SP_KEY_USER_INFO_FOR_AUTO_LOGIN = "SP_KEY_USER_INFO_FOR_AUTO_LOGIN";

	private final static String SP_VALUE_SEPERATOR = "@###@";

	public static boolean hasFroyo() {
		// Can use static final constants like FROYO, declared in later versions
		// of the OS since they are inlined at compile time. This is guaranteed
		// behavior.
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
	}

	public static boolean hasHoneycomb() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
	}

	public static void recycleMemory() {
		System.gc();
	}
	
	/**
	 * get current selected city information.
	 * 
	 * @return city information.
	 */
	public static CityBean getSelectedCityInfo() {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		String citys = sp.getString(SP_KEY_SELECTED_CITY, "");
		String[] city = sp.getString(SP_KEY_SELECTED_CITY, "").split(
				SP_VALUE_SEPERATOR);
		Log.d(TAG, "city = [" + citys + "]" + ", len=" + city.length);
		if (city.length <= 1) {
			return null;
		} else {
			CityBean info = new CityBean();
			info.setCityId(city[0]);
			info.setCityName(city[1]);
			info.setIsHot(Boolean.valueOf(city[2]));
			info.setStoreNumber(city[3]);
			info.setAlpha(city[4]);
			info.setJianpin(city[5]);
			return info;
		}
	}

	/**
	 * save selected city information.
	 * 
	 * @param city
	 *            selected city information.
	 */
	public static void setSelectedCityInfo(CityBean city) {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		Editor ed = sp.edit();
		String cityInfo = city.getCityId() + SP_VALUE_SEPERATOR
				+ city.getCityName() + SP_VALUE_SEPERATOR + city.getIsHot()
				+ SP_VALUE_SEPERATOR + city.getStoreNumber()
				+ SP_VALUE_SEPERATOR + city.getAlpha() + SP_VALUE_SEPERATOR
				+ city.getJianpin();
		Log.d(TAG, "setSelectedCityInfo: (" + cityInfo + ")");
		ed.putString(SP_KEY_SELECTED_CITY, cityInfo);
		ed.commit();
	}

	/**
	 * get current selected store type information.
	 * 
	 * @return store type information.
	 */
	public static StoreTypeInfo getSelectedStoreType() {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		String type = sp.getString(SP_KEY_SELECTED_STORE_TYPE, "");
		String[] types = sp.getString(SP_KEY_SELECTED_STORE_TYPE, "").split(
				SP_VALUE_SEPERATOR);
		Log.d(TAG, "getSelectedStoreType: [" + type + "]" + ", len="
				+ types.length);
		StoreTypeInfo info = new StoreTypeInfo();
		if (types.length <= 1) {
			return null;
//			info.setTypeId(CommonValue.STORE_TYPE_ID_ALL);
//			info.setTypeName("All");
		} else {
			info.setTypeId(types[0]);
			info.setTypeName(types[1]);
		}

		return info;
	}

	/**
	 * save current selected store type information.
	 * 
	 * @param info
	 *            selected store type information.
	 */
	public static void setSelectedStoreTypeInfo(StoreTypeInfo info) {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		Editor ed = sp.edit();
		String typeInfo = info.getTypeId() + SP_VALUE_SEPERATOR
				+ info.getTypeName();
		ed.putString(SP_KEY_SELECTED_STORE_TYPE, typeInfo);
		ed.commit();
	}

	/**
	 * save current displayed page index.
	 * 
	 * @param index
	 *            current displayed page index.
	 */
	public static void setCurrentPageIndex(int index) {
		Log.d(TAG, "setCurrentPageIndex = " + index);
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		Editor ed = sp.edit();
		ed.putInt(SP_KEY_SELECTED_PAGE_INDEX, index);
		ed.commit();
	}

	/**
	 * get current displayed page index.
	 * 
	 * @return page index.
	 */
	public static int getCurrentPageIndex() {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		final int index = sp.getInt(SP_KEY_SELECTED_PAGE_INDEX,
				CommonValue.EnumPageIndex.EPageStoreList);
		Log.d(TAG, "getCurrentPageIndex = " + index);
		return index;
	}

	/**
	 * check is there user log in or not.
	 * 
	 * @return true means someone is login otherwise nobody log in.
	 */
	public static boolean isUserLogIn() {
		return !CommonValue.STR_EMPTY.equals(getCurrentLoginUserMobile());
	}

	/**
	 * get current log in user's mobile.
	 * 
	 * @return mobile for current log in user.
	 */
	public static String getCurrentLoginUserMobile() {
		return getCurrentLogInUser().getUsrMobile();
	}

	/**
	 * save current login user information into local file system.
	 * 
	 * @param info
	 *            user information.
	 */
	public static void saveCurrentLogInUser(UserInfo info) {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		Editor ed = sp.edit();
		if (null == info) {
			String usr = "";
			ed.putString(SP_KEY_CURRENT_LOGIN_USER, usr);
			ed.commit();
		} else {
			String usr = info.getUsrId() + SP_VALUE_SEPERATOR
					+ info.getUsrName() + SP_VALUE_SEPERATOR
					+ info.getUsrHeadIconUrl()+ SP_VALUE_SEPERATOR
					+ info.getUsrMobile();
			ed.putString(SP_KEY_CURRENT_LOGIN_USER, usr);
			ed.commit();
		}

	}

	/**
	 * get current login user information.
	 * 
	 * @return user information.
	 */
	public static UserInfo getCurrentLogInUser() {
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		String[] info = sp.getString(SP_KEY_CURRENT_LOGIN_USER, "").split(
				SP_VALUE_SEPERATOR);
		UserInfo usr = new UserInfo();
		Log.d(TAG,
				"getCurrentLogInUser: "
						+ sp.getString(SP_KEY_CURRENT_LOGIN_USER, ""));
		if (info.length <= 1) {
			usr.setUsrId("-1");
			usr.setUsrName(CommonValue.STR_EMPTY);
			usr.setUsrMobile(CommonValue.STR_EMPTY);
			usr.setUsrHeadIconUrl(CommonValue.STR_EMPTY);
		} else {
			usr.setUsrId(info[0]);
			usr.setUsrName(info[1]);
			usr.setUsrHeadIconUrl(info[2]);
			usr.setUsrMobile(info[3]);
			
		}

		return usr;
	}

	/**
	 * auto log in
	 * 
	 * @param user
	 *            mobile
	 * @param user
	 *            passwords
	 * @param whether
	 *            auto login
	 */
	public static void saveUsrInfoForAutoLogin(String user) {

		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		Editor ed = sp.edit();
		/*
		 * String _info = user + SP_VALUE_SEPERATOR + pw + SP_VALUE_SEPERATOR +
		 * isAuto;
		 */
		Log.d(TAG, "usr info ===> " + user);
		ed.putString(SP_KEY_USER_INFO_FOR_AUTO_LOGIN, user);
		ed.commit();
	}

	/**
	 * return auto login infomations
	 * 
	 * @return string[]{userMobile, UserPassword, IsAutoLogin}
	 */
	public static String getUsrInfoForAutoLogin() {
		String usrLoginInfo = null;
		SharedPreferences sp = UtilityHelper.instance().getSharedPreferences(
				CommonValue.SP_CONFIG);
		// usrLoginInfo =
		// sp.getString(SP_KEY_USER_INFO_FOR_AUTO_LOGIN,"").split(SP_VALUE_SEPERATOR);
		usrLoginInfo = sp.getString(SP_KEY_USER_INFO_FOR_AUTO_LOGIN, "");
		return usrLoginInfo;
	}

	public static int getCurrentYear() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR);
	}
}
